#ifndef __UDF_GLOBAL_H
#define __UDF_GLOBAL_H

#include <stdint.h>

#include "sdk_ifs_udk_cfg.h"
#include "udp_def.h"
#include "develop_def.h"
#include "udf_global_sys.h"

#include "udf_cfg.h"
#include "udp_def.h"

/* 用户宏定义 */
#define FW_VERSION_CAT(a,b) a##_30.10.8.##b\n
#define MACRO_STR(x) #x
#define TO_STR(x) MACRO_STR(x)

#if (POINTRST_USER_PROCESS == 1)
#ifdef ANA4_VDD12_DIS
#undef ANA4_VDD12_DIS
#define ANA4_VDD12_DIS (0) // 休眠不关闭BBE SRAM电源
#endif
#endif

#define UDFDEF_ANT2_CALIB_R (10541)
#define UDFDEF_ANT2_CALIB_I (45)

#define UDPCFG_SYNCW                        ( 0x0000FF00 )
#define UDPCFG_CHIRPW                       ( 0x000000FF )

#ifdef TEST_RF_PW
#define UDPCFG_RAMP_WM                      ( UDF_CFG_RAMP_WAVE0_WAVE1_INF )
#else
#define UDPCFG_RAMP_WM                      ( UDP_CFG_RAMP_WAVE0_WAVE1_32_TIMES )
#endif
#define UDFDEF_RAMP_CNT                     ( UDFDEF_CHIRP_SUM_NUM )
#define UDFDEF_CYCLE_NUM                    ( UDFDEF_CHIRP_NUM )
																				   
#define UDFDEF_ADCSAMP_NUMLOG               ( NUMLOG_64 )
#define UDFDEF_ADCSAMP_NUM                  ( 1<<UDFDEF_ADCSAMP_NUMLOG )
																	        
#define UDFDEF_CHIRP_NUM                    ( DEMO_CHIRP_NUM )  // max:32
#define UDFDEF_ANT_NUM                      ( DEMO_RX_ANT_NUM )
#define UDFDEF_VEL_NUM                      ( UDFDEF_CHIRP_NUM )


#if ULTRA_LPW == 1
#define UDFDEF_CHIRP_SUM_NUM                ( 1 )
#endif                                      
																		        
#if (RAMP_BANDWIDTH == 0)                   
#define UDFDEF_ADCSAMP_OFFSET               ( 4 )
#define UDFDEF_RANGE_USE                    ( 40 ) // max:40
#if !defined(UDFDEF_CHIRP_SUM_NUM)          
#define UDFDEF_CHIRP_SUM_NUM                ( 8 ) // default:8
#endif                                      
#define RANGE_RES_MM                        ( 220 )
#define VEL_RES_MM_S                        ( 233 )
#elif (RAMP_BANDWIDTH == 1)                 
#define UDFDEF_ADCSAMP_OFFSET               ( 10 )
#define UDFDEF_RANGE_USE                    ( 32 ) // adc:64 fft:64-> max:32
#if !defined(UDFDEF_CHIRP_SUM_NUM)          
#define UDFDEF_CHIRP_SUM_NUM                ( 5 ) // max:24 default:5
#endif                                      
#define RANGE_RES_MM                        ( 220 )
#define VEL_RES_MM_S                        ( 366 )
#else
#error xxxx
#endif

#if (RAMP_BANDWIDTH==0) && (BOARD_ID==0)
#include "udf_global_bd_usr_250M.h"
#elif (RAMP_BANDWIDTH==1) && (BOARD_ID==0)
#include "udf_global_bd_usr_800M.h"
#elif (RAMP_BANDWIDTH==0) && (BOARD_ID==1)
#include "udf_global_bd_evb_250M.h"
#elif (RAMP_BANDWIDTH==1) && (BOARD_ID==1)
#include "udf_global_bd_evb_800M.h"
#elif (RAMP_BANDWIDTH==0) && (BOARD_ID==2)
#include "udf_global_bd_evm_1t2r_250M.h"
#elif (RAMP_BANDWIDTH==1) && (BOARD_ID==2)
#include "udf_global_bd_evm_1t2r_800M.h"
#elif (RAMP_BANDWIDTH==0) && (BOARD_ID==3)
#include "udf_global_bd_evm_1t1r_250M.h"
#elif (RAMP_BANDWIDTH==1) && (BOARD_ID==3)
#include "udf_global_bd_evm_1t1r_800M.h"
#elif (RAMP_BANDWIDTH==0) && (BOARD_ID==4)
#include "udf_global_bd_evm2_1t2r_250M.h"
#elif (RAMP_BANDWIDTH==1) && (BOARD_ID==4)
#include "udf_global_bd_evm2_1t2r_800M.h"
#else
#error unknown BOARD_ID or RAMP_BANDWIDTH
#endif

#if 1 /* BBE地址分配  */
#define UDFDEF_SIZE_FFT1D                   (UDFDEF_RANGE_USE*NUM_BYTE32)

/* stage:0 adc->chirpsum_ant1/ant2->fft1d_ant1/ant2*/
/*adc */
#define UDFDEF_ADDR_ADCSAMP                 (BBE_ABUF2_BASE + 0)  // max:6k
#if (TEST_MODE == 4 )
#define UDFDEF_ADDR_ADCSAMP_TEST            (BBE_ABUF0_BASE + 0)
#endif

/* stage:1 1d->2d->3d pre*/
/*fft1d */
#define UDFDEF_ADDR_FFT1D_ANT1              (BBE_ABUF0_BASE + 0 )
#define UDFDEF_ADDR_FFT1D_ANT2              (BBE_ABUF1_BASE + 0 )
#define UDFDEF_ADDR_CHIRPSUM_ANT1           (UDFDEF_ADDR_FFT1D_ANT1 + UDFDEF_SIZE_FFT1D*UDFDEF_CHIRP_NUM )
#define UDFDEF_ADDR_CHIRPSUM_ANT2           (UDFDEF_ADDR_FFT1D_ANT2 + UDFDEF_SIZE_FFT1D*UDFDEF_CHIRP_NUM )
#define UDFDEF_ADDR_FFT2D_ANT1              (UDFDEF_ADDR_FFT1D_ANT1 )
#define UDFDEF_ADDR_FFT2D_ANT2              (UDFDEF_ADDR_FFT1D_ANT2 )

#define UDFDEF_ADDR_PRESENCE_FFT1D_ANT1     (BBE_ABUF0_BASE + 8*1024 )
#define UDFDEF_ADDR_PRESENCE_FFT1D_ANT2     (BBE_ABUF1_BASE + 8*1024 )
#define UDFDEF_ADDR_PRESENCE_FFT2D_ANT1     (BBE_ABUF0_BASE + 0*1024 )
#define UDFDEF_ADDR_PRESENCE_FFT2D_ANT2     (BBE_ABUF1_BASE + 0*1024 )

/*uart output buffer*/
#define UDFDEF_ADDR_UART_OUTPUT             (BBE_ABUF0_BASE + 0)
/*uart debug buffer*/
#define UDFDEF_ADDR_DEBUG_OUTPUT            (BBE_ABUF0_BASE + 0) // not used

#define UDFDEF_SIZE_WIN_VEL                 ( UDFDEF_CHIRP_NUM*2 )
#define UDFDEF_ADDR_TEMP                    ( BBE_ABUF2_BASE + 0*1024 )
#define UDFDEF_SIZE_TEMP                    ( UDFDEF_SIZE_WIN_VEL )
#define UDFDEF_ADDR_WIN_VEL_TEMP_ANT1       ( BBE_ABUF1_BASE + 16*1024 - UDFDEF_SIZE_TEMP )
#define UDFDEF_ADDR_WIN_VEL_TEMP_ANT2       ( BBE_ABUF0_BASE )

// 上帧均值
#define UDFDEF_ADDR_FFT1D_MEAN_LAST         (BBE_ABUF2_BASE + 0*1024) // UDFDEF_SIZE_FFT1D

// 加权帧均值
#define UDFDEF_ADDR_BACKUP_AREA0            (UDFDEF_ADDR_FFT1D_MEAN_LAST + UDFDEF_SIZE_FFT1D )     // UDFDEF_SIZE_FFT1D+4 max:256+4
#define UDFDEF_ADDR_BACKUP_AREA1            (UDFDEF_ADDR_BACKUP_AREA0    + UDFDEF_SIZE_FFT1D + 4)  // UDFDEF_SIZE_FFT1D+4 max:256+4
#define UDFDEF_ADDR_ABUF_MEAN0              (UDFDEF_ADDR_BACKUP_AREA1    + UDFDEF_SIZE_FFT1D + 4)  // max:256
#define UDFDEF_ADDR_ABUF_MEAN1              (UDFDEF_ADDR_ABUF_MEAN0      + UDFDEF_SIZE_FFT1D )     // max:256
#define UDFDEF_ADDR_MEAN0_COEFF_RST         (BBE_ABUF0_BASE)
#define UDFDEF_ADDR_MEAN1_COEFF_RST         (BBE_ABUF1_BASE + 15*1024)
#define UDFDEF_ADDR_MEAN0_COEF              (BBE_ABUF1_BASE + 15*1024 + UDFDEF_SIZE_FFT1D)
#define UDFDEF_ADDR_MEAN1_COEF              (BBE_ABUF0_BASE + UDFDEF_SIZE_FFT1D)

// 备份帧均值
#define UDFDEF_ADDR_SRAM_MEAN_ANT1          ((uint8_t*)&sUDF_global.buff[0])
#define UDFDEF_ADDR_SRAM_MEAN_ANT2          ((uint8_t*)&sUDF_global.buff[1])

/*加权过程可能覆盖UDFDEF_ADDR_CFAR1_CACHE,无妨*/
#define UDFDEF_ADDR_CFAR1_CACHE             ( BBE_ABUF2_BASE + 0*1024 + 128 )                     // 512(CFAR_MAX_NUM*NUM_BYTE32)
#define UDFDEF_ADDR_CFAR2_CACHE             ( UDFDEF_ADDR_CFAR1_CACHE + CFAR_MAX_NUM*NUM_BYTE32)   // 512(CFAR_MAX_NUM*NUM_BYTE32)

/*stage 1 持久↓*/
#define UDFDEF_ADDR_FFT1D_MEAN              ( UDFDEF_ADDR_CFAR2_CACHE + CFAR_MAX_NUM*NUM_BYTE32 ) // UDFDEF_RANGE_USE*NUM_BYTE32  max:256
#define UDFDEF_ADDR_FFT3D_PRE               ( UDFDEF_ADDR_FFT1D_MEAN + 256 ) // CFAR_MAX_NUM*MAX_ANT_NUM*NUM_BYTE32 1024
#define UDFDEF_ADDR_FFT3D_PRE_PRESENCE      ( UDFDEF_ADDR_FFT3D_PRE + 1024 ) // CFAR_MAX_NUM*MAX_ANT_NUM*NUM_BYTE32 1024

/*stage 1、stage2、stage3 持久↓*/
#define UDFDEF_ADDR_POINT_RST               (UDFDEF_ADDR_FFT3D_PRE_PRESENCE + 1024)               // 1K+4B(FRAME_MAX_NUM*8+4)
#define UDFDEF_ADDR_POINT_RST_PRESENCE      (UDFDEF_ADDR_POINT_RST   + FRAME_MAX_NUM*8+4)         // 1K+4B(FRAME_MAX_NUM*8+4)
#define UDFDEF_ADDR_POINT_RST_TEMP          (UDFDEF_ADDR_POINT_RST_PRESENCE   + FRAME_MAX_NUM*8+4)  // 1K+4B(FRAME_MAX_NUM*8+4)

/*stage 0、stage1、stage2 持久↓*/
#define UDFDEF_ADDR_ARRAY_CALIB             (UDFDEF_ADDR_POINT_RST_PRESENCE  + FRAME_MAX_NUM*8+4) // 512(64*4*2)
#define UDFDEF_ADDR_WIN_RANGE               (UDFDEF_ADDR_ARRAY_CALIB + 512 )                      // 512(WIN_MAX_LEN*NUM_BYTE16)
#define UDFDEF_ADDR_WIN_VEL                 (UDFDEF_ADDR_WIN_RANGE   + WIN_MAX_LEN*NUM_BYTE16)    // 512(WIN_MAX_LEN*NUM_BYTE16)

/* stage:2 3d */
#define UDFDEF_ADDR_ANGLE_RSLT              ( BBE_ABUF0_BASE + 0*1024 )
#define UDFDEF_ADDR_ARRAY_CALIB_RUN         ( BBE_ABUF1_BASE + 0*1024 )

/* stage:3 后处理 */
// 除UDFDEF_ADDR_POINT_RST_MERGE地址起始1K+4B外 
// ABUF0(0~8*1024)、ABUF1(0~8*1024)地址临时可用
// ABUF0/ABUF1( (16*1024-(64-UDFDEF_RANGE_USE)*UDFDEF_CHIRP_NUM*4) ~ 16*1024 )可存储持久化数据 
// 即( --> 临时ram：abuf0:0~8K abuf1:1K4B~8K  持久ram：abuf0:13K~16K  abuf1:13K~16K )

// 临时使用
#define UDFDEF_ADDR_POINT_RST_MERGE            ( BBE_ABUF1_BASE + 0*1024 )

#define UDFDEF_ADDR_EPC_STRUCT                 ( BBE_ABUF1_BASE + 2*1024 )
#define UDFDEF_SIZE_EPC_STRUCT                 ( 2*1024 )
#define UDFDEF_ADDR_DBSCAN_BASE                ( BBE_ABUF0_BASE + 4*1024 )
#define UDFDEF_SIZE_DBSCAN_STRUCT              ( 4*1024 )

// 持久化使用
#define UDFDEF_ADDR_NONVOLATILE_PCRST_MOTION   ( BBE_ABUF0_BASE + 13*1024 )
#define UDFDEF_ADDR_NONVOLATILE_PCRST_PRESENCE ( BBE_ABUF0_BASE + 14*1024 + 4 )
#define UDFDEF_ADDR_HOLD_STRUCT                ( BBE_ABUF1_BASE + 13*1024 )
#define UDFDEF_SIZE_HOLD_STRUCT                ( 1*1024 )
#define UDFDEF_ADDR_FILTER_STRUCT              ( UDFDEF_ADDR_HOLD_STRUCT + UDFDEF_SIZE_HOLD_STRUCT )
#define UDFDEF_SIZE_FILTER_STRUCT              ( 2*1024 )

//ota
#define OTA_STATUS_IDLE        (0)
#define OTA_STATUS_DOWNLOAD    (1)
#define OTA_STATUS_OTA_WRITE   (2)
#define OTA_STATUS_INFO_WRITE  (3)
#define OTA_MIND_TRANSMISSION  (4)
#define PAGE_PACK_LEN (256)

#endif
#define TRACK_MODE 1
#define N_CLUSTER 8 // cluster数目
#define N_TRACK 7    // track数目
#define V_MEAN 5
#define V_MAX 30
#define DELTA_MAX 100
#define PROTECT_TIME 10
#define RANGE_MAX 330
/** @brief  object has not been associated. */
#define NOT_ASSOCIATED  (-1)
/** @brief  object has been associated. */
#define IS_ASSOCIATED   (1)
/** @brief  object is invalid. */
#define IS_INVALID      (0)
/** @brief  object is valid. */
#define IS_VALID        (1)
typedef struct ClusterInfo_t
{
    uint16_t nPoint;
    uint8_t maxDopIdx;
    int8_t associateFlag;
    int32_t rSum;
    int32_t xSum;
    uint32_t ySum;
    uint32_t rdPowSum;
    uint32_t DopIdxSum;
    uint32_t DopIdxMean;
    uint32_t rdPowMean;
    int32_t rCenter;
    int16_t xCenter;
    uint16_t yCenter;
} ClusterInfo; // 40B

typedef struct ClusterList_t
{
    uint32_t nCluster;
    ClusterInfo info[N_CLUSTER];
} ClusterList;

typedef struct CtrackInfo_t
{
    uint8_t validity; 			/* 本跟踪是否有效 */
    uint8_t stateFlag;     /* track状态标记 */
    uint8_t stateSwapCnt;  /* track状态切换次数 */
    uint8_t moveDirection; /* 运动方向 */
    uint16_t loseCnt;      /* 连续消失次数 */
    uint16_t recordCnt;    /* 累计被记录次数 */
    uint16_t staticCnt; 	/* 运动方向 */
    uint8_t NoUpdateFlag; 	
    uint8_t associateR;
    int32_t r;
    int16_t x;  /* 坐标：径向距离（m) */
    int16_t y; /* 坐标：x坐标（m) */
    int16_t vx;  /* 坐标：径向距离（m) */
    int16_t vy; /* 坐标：x坐标（m) */
} CtrackInfo;   // 20B

typedef struct Track_Info_t
{
	uint32_t track_sign;
	uint32_t track_cnt;
	uint32_t lost_cnt;
	uint32_t track_startCnt;
	int32_t track_r;// cm
	int32_t track_v;// cm/s
	int32_t track_a;// *100; angleId*100*2/NUM_ANGLE; sinAzim
	int16_t track_rY;// cm
}TrackInfo;

typedef struct MaxPowerInfo
{
	uint16_t targetNum;
	uint32_t maxPow;
	int32_t maxPowR;// cm
	int32_t maxPowV;// cm/s
	int32_t maxPowA;// *100; angleId*100*2/NUM_ANGLE; sinAzim
	uint32_t maxPowSign;
}MaxPowPoint;
typedef struct Track_XY
{
	int32_t r;
	int16_t x;
	uint16_t y;// cm
}TrackInfo_XY;


typedef struct CtrackInfo_tmp   //存储结果buff
{
	int32_t r;
	int16_t x;
	int16_t y;
} CtrackInfo_t;

struct CtrackList_tmp
{
	uint16_t status;
	uint16_t nTrack;
	CtrackInfo_t info[N_TRACK];
} ;


typedef struct Presence_Flag_Info_tmp //静态标志buff
{
	uint8_t flag;
} Presence_Flag_Info;			
struct Presence_Flag_List_temp
{
	Presence_Flag_Info info[N_TRACK];
};


typedef struct CtrackList_t
{
	uint32_t nTrack;
	CtrackInfo info[N_TRACK];
} CtrackList;
#if 1 /* 全局变量定义 */
// 必须保证UDF_global结构体类型有定义 
// magic_flag_A、magic_flag_B为构建需要初始值不可修改，且位置必须位于结构体最后。
typedef struct{
	int pcrst_freq_cnt;	
	uint32_t frame_cnt;


	#if (FFT2D_MINUS_MEAN_MODE == 1)
	uint16_t minus_old_frame_enable;
	uint16_t no_target_cnt;
	#endif
	
	#if defined (GPIO_OUTPUT_ENABLE)
	uint32_t gpio_output_lck;
	#endif

	#if defined(PRESENCE_ENABLE)
	//int chirp_fifo_index;
	#endif

	#if ( FFT2D_MINUS_MEAN_MODE > 0 )  
	uint32_t buff[2][UDFDEF_RANGE_USE];
	#endif
	uint32_t recv_buff[PAGE_PACK_LEN/sizeof(uint32_t)];
	uint8_t ota_status;
	uint32_t ota_len;
	uint32_t mind_len;
  uint32_t mind_CRC32;
  uint8_t mind_CRC8;
  uint32_t mind_pack;
	
	TrackInfo track;
	
	uint32_t magic_flag_A;
	uint32_t magic_flag_B;
}sUDF_global_t;

extern sUDF_global_t sUDF_global;
#endif



#if 1 /*UDSF功能宏定义*/
#define UDSF_DATA_ACQ_SPECIAL (1)

#endif

#endif
